namespace DotNetNuke.NavigationControl
{
    using DotNetNuke.Modules.NavigationProvider;
    using DotNetNuke.UI.WebControls;

    
    using System;
    using System.Collections;
    using System.Web.UI;

    public class DNNMenuNavigationProvider : DotNetNuke.Modules.NavigationProvider.NavigationProvider
    {
        private bool m_blnIndicateChildren;
        private DNNMenu m_objMenu;
        private string m_strControlID;
        private string m_strCSSBreadCrumbRoot;
        private string m_strCSSBreadCrumbSub;
        private string m_strCSSLeftSeparator;
        private string m_strCSSLeftSeparatorBreadCrumb;
        private string m_strCSSLeftSeparatorSelection;
        private string m_strCSSNodeHoverRoot;
        private string m_strCSSNodeHoverSub;
        private string m_strCSSNodeRoot;
        private string m_strCSSRightSeparator;
        private string m_strCSSRightSeparatorBreadCrumb;
        private string m_strCSSRightSeparatorSelection;
        private string m_strCSSSeparator;
        private string m_strIndicateChildImageBreadCrumbRoot;
        private string m_strIndicateChildImageBreadCrumbSub;
        private string m_strNodeLeftHTMLBreadCrumbRoot = "";
        private string m_strNodeLeftHTMLBreadCrumbSub = "";
        private string m_strNodeLeftHTMLRoot = "";
        private string m_strNodeLeftHTMLSub = "";
        private string m_strNodeRightHTMLBreadCrumbRoot = "";
        private string m_strNodeRightHTMLBreadCrumbSub = "";
        private string m_strNodeRightHTMLRoot = "";
        private string m_strNodeRightHTMLSub = "";
        private string m_strNodeSelectedRoot;
        private string m_strNodeSelectedSub;
        private string m_strPathImage;
        private string m_strSeparatorHTML = "";
        private string m_strSeparatorLeftHTML = "";
        private string m_strSeparatorLeftHTMLActive = "";
        private string m_strSeparatorLeftHTMLBreadCrumb = "";
        private string m_strSeparatorRightHTML = "";
        private string m_strSeparatorRightHTMLActive = "";
        private string m_strSeparatorRightHTMLBreadCrumb = "";

        private void AddSeparator(string strType, DNNNode objPrevNode, DNNNode objNextNode)
        {
            string strLeftHTML = this.SeparatorLeftHTML + this.SeparatorLeftHTMLBreadCrumb + this.SeparatorLeftHTMLActive;
            string strRightHTML = this.SeparatorRightHTML + this.SeparatorRightHTMLBreadCrumb + this.SeparatorRightHTMLActive;
            if ((this.SeparatorHTML + strLeftHTML + strRightHTML).Length > 0)
            {
                string strSeparatorTable = "";
                string strSeparator = "";
                string strSeparatorLeftHTML = "";
                string strSeparatorRightHTML = "";
                string strSeparatorClass = "";
                string strLeftSeparatorClass = "";
                string strRightSeparatorClass = "";
                if (strLeftHTML.Length > 0)
                {
                    strLeftSeparatorClass = this.GetSeparatorText(this.CSSLeftSeparator, this.CSSLeftSeparatorBreadCrumb, this.CSSLeftSeparatorSelection, objNextNode);
                    strSeparatorLeftHTML = this.GetSeparatorText(this.SeparatorLeftHTML, this.SeparatorLeftHTMLBreadCrumb, this.SeparatorLeftHTMLActive, objNextNode);
                }
                if (this.SeparatorHTML != "")
                {
                    if (this.CSSSeparator != "")
                    {
                        strSeparatorClass = this.CSSSeparator;
                    }
                    strSeparator = this.SeparatorHTML;
                }
                if (strRightHTML.Length > 0)
                {
                    strRightSeparatorClass = this.GetSeparatorText(this.CSSRightSeparator, this.CSSRightSeparatorBreadCrumb, this.CSSRightSeparatorSelection, objPrevNode);
                    strSeparatorRightHTML = this.GetSeparatorText(this.SeparatorRightHTML, this.SeparatorRightHTMLBreadCrumb, this.SeparatorRightHTMLActive, objPrevNode);
                }
                strSeparatorTable = "<table summary=\"Table for menu separator design\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
                if ((((strSeparatorRightHTML != "") && (strType != "Left")) ? 1 : 0) != 0)
                {
                    strSeparatorTable = strSeparatorTable + this.GetSeparatorTD(strRightSeparatorClass, strSeparatorRightHTML);
                }
                if (((((strSeparator != "") && (strType != "Left")) && (strType != "Right")) ? 1 : 0) != 0)
                {
                    strSeparatorTable = strSeparatorTable + this.GetSeparatorTD(strSeparatorClass, strSeparator);
                }
                if ((((strSeparatorLeftHTML != "") && (strType != "Right")) ? 1 : 0) != 0)
                {
                    strSeparatorTable = strSeparatorTable + this.GetSeparatorTD(strLeftSeparatorClass, strSeparatorLeftHTML);
                }
                strSeparatorTable = strSeparatorTable + "</tr></table>";
            }
        }

        public override void Bind(DNNNodeCollection objNodes)
        {
            DNNNode objNode = null;
            DNNNode objPrevNode = null;
            IEnumerator refObjectHelperL0=null;
            bool RootFlag=false;
            if (!this.IndicateChildren)
            {
                this.IndicateChildImageSub = "";
                this.IndicateChildImageRoot = "";
            }
            if ((((this.CSSNodeSelectedRoot.Length > 0) && (this.CSSNodeSelectedRoot == this.CSSNodeSelectedSub)) ? 1 : 0) != 0)
            {
                this.Menu.DefaultNodeCssClassSelected = this.CSSNodeSelectedRoot;
            }
            try
            {
                refObjectHelperL0 = objNodes.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    MenuNode objMenuItem;
                    objNode = (DNNNode) refObjectHelperL0.Current;
                    if (objNode.Level == 0)
                    {
                        if (RootFlag)
                        {
                            this.AddSeparator("All", objPrevNode, objNode);
                        }
                        else
                        {
                            if ((this.SeparatorLeftHTML == "") && (this.SeparatorLeftHTMLBreadCrumb == ""))
                            {
                            }
                            if (((this.SeparatorLeftHTMLActive != "") ? 1 : 0) != 0)
                            {
                                this.AddSeparator("Left", objPrevNode, objNode);
                            }
                            RootFlag = true;
                        }
                        int intIndex = this.Menu.MenuNodes.Import(objNode, false);
                        objMenuItem = this.Menu.MenuNodes[intIndex];
                        if (this.CSSNodeRoot != "")
                        {
                            objMenuItem.CSSClass = this.CSSNodeRoot;
                        }
                        if (this.CSSNodeHoverRoot != "")
                        {
                            objMenuItem.CSSClassSelected = this.CSSNodeHoverRoot;
                        }
                        if (this.NodeLeftHTMLRoot != "")
                        {
                            objMenuItem.LeftHTML = this.NodeLeftHTMLRoot;
                        }
                        objMenuItem.CSSIcon = " ";
                        if (objNode.BreadCrumb)
                        {
                            objMenuItem.CSSClass = this.CSSBreadCrumbRoot;
                            if (this.NodeLeftHTMLBreadCrumbRoot != "")
                            {
                                objMenuItem.LeftHTML = this.NodeLeftHTMLBreadCrumbRoot;
                            }
                            if (this.NodeRightHTMLBreadCrumbRoot != "")
                            {
                                objMenuItem.RightHTML = this.NodeRightHTMLBreadCrumbRoot;
                            }
                        }
                        if (this.NodeRightHTMLRoot != "")
                        {
                            objMenuItem.RightHTML = this.NodeRightHTMLRoot;
                        }
                    }
                    else
                    {
                        try
                        {
                            MenuNode objParent = this.Menu.MenuNodes.FindNode(objNode.ParentNode.ID);
                            if (objParent == null)
                            {
                                objParent = this.Menu.MenuNodes[this.Menu.MenuNodes.Import(objNode.ParentNode.Clone(), true)];
                            }
                            objMenuItem = objParent.MenuNodes.FindNode(objNode.ID);
                            if (objMenuItem == null)
                            {
                                objMenuItem = objParent.MenuNodes[objParent.MenuNodes.Import(objNode.Clone(), true)];
                            }
                            if (this.CSSNodeHover != "")
                            {
                                objMenuItem.CSSClassSelected = this.CSSNodeHover;
                            }
                            if (this.NodeLeftHTMLSub != "")
                            {
                                objMenuItem.LeftHTML = this.NodeLeftHTMLSub;
                            }
                            if (objNode.BreadCrumb)
                            {
                                objMenuItem.CSSClass = this.CSSBreadCrumbRoot;
                                if (this.NodeLeftHTMLBreadCrumbRoot != "")
                                {
                                    objMenuItem.LeftHTML = this.NodeLeftHTMLBreadCrumbRoot;
                                }
                                if (this.NodeRightHTMLBreadCrumbRoot != "")
                                {
                                    objMenuItem.RightHTML = this.NodeRightHTMLBreadCrumbRoot;
                                }
                                if (objNode.Selected)
                                {
                                    objMenuItem.CSSClass = this.CSSNodeSelectedRoot;
                                }
                            }
                            if (this.NodeRightHTMLSub != "")
                            {
                                objMenuItem.RightHTML = this.NodeRightHTMLSub;
                            }
                        }
                        catch (Exception exception1)
                        {
            
                            objMenuItem = null;
            
                        }
                    }
                    if (objNode.Image.Length > 0)
                    {
                        if (((!objNode.Image.StartsWith("/") && (this.PathImage.Length > 0)) ? 1 : 0) != 0)
                        {
                            objNode.Image = this.PathImage + objNode.Image;
                        }
                        objMenuItem.Image = objNode.Image;
                    }
                    objMenuItem.ToolTip = objNode.ToolTip;
                    this.Bind(objNode.DNNNodes);
                    objPrevNode = objNode;
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            if ((((objNode != null) && (objNode.Level == 0)) ? 1 : 0) != 0)
            {
                if ((this.SeparatorRightHTML == "") && (this.SeparatorRightHTMLBreadCrumb == ""))
                {
                }
                if (((this.SeparatorRightHTMLActive != "") ? 1 : 0) != 0)
                {
                    this.AddSeparator("Right", objPrevNode, null);
                }
                if (this.IndicateChildImageSub.Length > 0)
                {
                    this.IndicateChildImageSub = this.PathSystemImage + this.IndicateChildImageSub;
                }
                if (this.IndicateChildImageRoot.Length > 0)
                {
                    this.IndicateChildImageRoot = this.PathSystemImage + this.IndicateChildImageRoot;
                }
            }
        }

        public override void ClearNodes()
        {
            this.Menu.MenuNodes.Clear();
        }

        private void DNNMenu_NodeClick(object source, DNNMenuNodeClickEventArgs e)
        {
            base.RaiseEvent_NodeClick(e.Node);
        }

        private void DNNMenu_PopulateOnDemand(object source, DNNMenuEventArgs e)
        {
            base.RaiseEvent_PopulateOnDemand(e.Node);
        }

        private string GetSeparatorTD(string strClass, string strHTML)
        {
            string strRet = "";
            if (strClass != "")
            {
                strRet = strRet + "<td class = \"" + strClass + "\">";
            }
            else
            {
                strRet = strRet + "<td>";
            }
            return (strRet + strHTML + "</td>");
        }

        private string GetSeparatorText(string strNormal, string strBreadCrumb, string strSelection, DNNNode objNode)
        {
            string strRet = "";
            if (strNormal != "")
            {
                strRet = strNormal;
            }
            if (((((strBreadCrumb != "") && (objNode != null)) && objNode.BreadCrumb) ? 1 : 0) != 0)
            {
                strRet = strBreadCrumb;
            }
            if (((((strSelection != "") && (objNode != null)) && objNode.Selected) ? 1 : 0) != 0)
            {
                strRet = strSelection;
            }
            return strRet;
        }

        public override void Initialize()
        {
            this.m_objMenu = new DNNMenu();
            this.Menu.ID = this.m_strControlID;
            this.Menu.NodeClick += new DNNMenu.DNNMenuNodeClickHandler(this.DNNMenu_NodeClick);
            this.Menu.PopulateOnDemand += new DNNMenu.DNNMenuEventHandler(this.DNNMenu_PopulateOnDemand);
        }

        public override string ControlID
        {
            get
            {
                return this.m_strControlID;
            }
            set
            {
                this.m_strControlID = value;
            }
        }

        public override DotNetNuke.Modules.NavigationProvider.NavigationProvider.Orientation ControlOrientation
        {
            get
            {
                DotNetNuke.Modules.NavigationProvider.NavigationProvider.Orientation ControlOrientation=NavigationProvider.Orientation.Horizontal;
                switch (this.Menu.Orientation)
                {
                    case DotNetNuke.UI.WebControls.Orientation.Horizontal:
                        return DotNetNuke.Modules.NavigationProvider.NavigationProvider.Orientation.Horizontal;

                    case DotNetNuke.UI.WebControls.Orientation.Vertical:
                        return DotNetNuke.Modules.NavigationProvider.NavigationProvider.Orientation.Vertical;
                }
                return ControlOrientation;
            }
            set
            {
                switch (((int) value))
                {
                    case 0:
                        this.Menu.Orientation = DotNetNuke.UI.WebControls.Orientation.Horizontal;
                        break;

                    case 1:
                        this.Menu.Orientation = DotNetNuke.UI.WebControls.Orientation.Vertical;
                        break;
                }
            }
        }

        public override string CSSBreadCrumbRoot
        {
            get
            {
                return this.m_strCSSBreadCrumbRoot;
            }
            set
            {
                this.m_strCSSBreadCrumbRoot = value;
            }
        }

        public override string CSSBreadCrumbSub
        {
            get
            {
                return this.m_strCSSBreadCrumbSub;
            }
            set
            {
                this.m_strCSSBreadCrumbSub = value;
            }
        }

        public override string CSSBreak
        {
            get
            {
                return "";
            }
            set
            {
            }
        }

        public override string CSSContainerSub
        {
            get
            {
                return this.Menu.MenuCssClass;
            }
            set
            {
                this.Menu.MenuCssClass = value;
            }
        }

        public override string CSSControl
        {
            get
            {
                return this.Menu.MenuBarCssClass;
            }
            set
            {
                this.Menu.MenuBarCssClass = value;
            }
        }

        public override string CSSIcon
        {
            get
            {
                return this.Menu.DefaultIconCssClass;
            }
            set
            {
                this.Menu.DefaultIconCssClass = value;
            }
        }

        public override string CSSIndicateChildRoot
        {
            get
            {
                return "";
            }
            set
            {
            }
        }

        public override string CSSIndicateChildSub
        {
            get
            {
                return "";
            }
            set
            {
            }
        }

        public override string CSSNode
        {
            get
            {
                return this.Menu.DefaultNodeCssClass;
            }
            set
            {
                this.Menu.DefaultNodeCssClass = value;
            }
        }

        public override string CSSNodeHover
        {
            get
            {
                return this.Menu.DefaultNodeCssClassOver;
            }
            set
            {
                this.Menu.DefaultNodeCssClassOver = value;
            }
        }

        public override string CSSNodeHoverRoot
        {
            get
            {
                return this.m_strCSSNodeHoverRoot;
            }
            set
            {
                this.m_strCSSNodeHoverRoot = value;
            }
        }

        public override string CSSNodeHoverSub
        {
            get
            {
                return this.m_strCSSNodeHoverSub;
            }
            set
            {
                this.m_strCSSNodeHoverSub = value;
            }
        }

        public override string CSSNodeRoot
        {
            get
            {
                return this.m_strCSSNodeRoot;
            }
            set
            {
                this.m_strCSSNodeRoot = value;
            }
        }

        public override string CSSNodeSelectedRoot
        {
            get
            {
                return this.m_strNodeSelectedRoot;
            }
            set
            {
                this.m_strNodeSelectedRoot = value;
            }
        }

        public override string CSSNodeSelectedSub
        {
            get
            {
                return this.m_strNodeSelectedSub;
            }
            set
            {
                this.m_strNodeSelectedSub = value;
            }
        }

        public override string ForceDownLevel
        {
            get
            {
                return this.Menu.ForceDownLevel.ToString();
            }
            set
            {
                this.Menu.ForceDownLevel = Convert.ToBoolean(value);
            }
        }

        public override string IndicateChildImageRoot
        {
            get
            {
                return this.Menu.RootArrowImage;
            }
            set
            {
                this.Menu.RootArrowImage = value;
            }
        }

        public override string IndicateChildImageSub
        {
            get
            {
                return this.Menu.ChildArrowImage;
            }
            set
            {
                this.Menu.ChildArrowImage = value;
            }
        }

        public override bool IndicateChildren
        {
            get
            {
                return this.m_blnIndicateChildren;
            }
            set
            {
                this.m_blnIndicateChildren = value;
            }
        }

        public DNNMenu Menu
        {
            get
            {
                return this.m_objMenu;
            }
        }

        public override Control NavigationControl
        {
            get
            {
                return this.Menu;
            }
        }

        public override string NodeLeftHTMLBreadCrumbRoot
        {
            get
            {
                return this.m_strNodeLeftHTMLBreadCrumbRoot;
            }
            set
            {
                this.m_strNodeLeftHTMLBreadCrumbRoot = value;
            }
        }

        public override string NodeLeftHTMLBreadCrumbSub
        {
            get
            {
                return this.m_strNodeLeftHTMLBreadCrumbSub;
            }
            set
            {
                this.m_strNodeLeftHTMLBreadCrumbSub = value;
            }
        }

        public override string NodeLeftHTMLRoot
        {
            get
            {
                return this.m_strNodeLeftHTMLRoot;
            }
            set
            {
                this.m_strNodeLeftHTMLRoot = value;
            }
        }

        public override string NodeLeftHTMLSub
        {
            get
            {
                return this.m_strNodeLeftHTMLSub;
            }
            set
            {
                this.m_strNodeLeftHTMLSub = value;
            }
        }

        public override string NodeRightHTMLBreadCrumbRoot
        {
            get
            {
                return this.m_strNodeRightHTMLBreadCrumbRoot;
            }
            set
            {
                this.m_strNodeRightHTMLBreadCrumbRoot = value;
            }
        }

        public override string NodeRightHTMLBreadCrumbSub
        {
            get
            {
                return this.m_strNodeRightHTMLBreadCrumbSub;
            }
            set
            {
                this.m_strNodeRightHTMLBreadCrumbSub = value;
            }
        }

        public override string NodeRightHTMLRoot
        {
            get
            {
                return this.m_strNodeRightHTMLRoot;
            }
            set
            {
                this.m_strNodeRightHTMLRoot = value;
            }
        }

        public override string NodeRightHTMLSub
        {
            get
            {
                return this.m_strNodeRightHTMLSub;
            }
            set
            {
                this.m_strNodeRightHTMLSub = value;
            }
        }

        public override string PathImage
        {
            get
            {
                return this.m_strPathImage;
            }
            set
            {
                this.m_strPathImage = value;
            }
        }

        public override string PathSystemImage
        {
            get
            {
                return this.Menu.SystemImagesPath;
            }
            set
            {
                this.Menu.SystemImagesPath = value;
            }
        }

        public override string PathSystemScript
        {
            get
            {
                return this.Menu.MenuScriptPath;
            }
            set
            {
            }
        }

        public override bool PopulateNodesFromClient
        {
            get
            {
                return this.Menu.PopulateNodesFromClient;
            }
            set
            {
                this.Menu.PopulateNodesFromClient = value;
            }
        }

        public override string SeparatorHTML
        {
            get
            {
                return this.m_strSeparatorHTML;
            }
            set
            {
                this.m_strSeparatorHTML = value;
            }
        }

        public override string SeparatorLeftHTML
        {
            get
            {
                return this.m_strSeparatorLeftHTML;
            }
            set
            {
                this.m_strSeparatorLeftHTML = value;
            }
        }

        public override string SeparatorLeftHTMLActive
        {
            get
            {
                return this.m_strSeparatorLeftHTMLActive;
            }
            set
            {
                this.m_strSeparatorLeftHTMLActive = value;
            }
        }

        public override string SeparatorLeftHTMLBreadCrumb
        {
            get
            {
                return this.m_strSeparatorLeftHTMLBreadCrumb;
            }
            set
            {
                this.m_strSeparatorLeftHTMLBreadCrumb = value;
            }
        }

        public override string SeparatorRightHTML
        {
            get
            {
                return this.m_strSeparatorRightHTML;
            }
            set
            {
                this.m_strSeparatorRightHTML = value;
            }
        }

        public override string SeparatorRightHTMLActive
        {
            get
            {
                return this.m_strSeparatorRightHTMLActive;
            }
            set
            {
                this.m_strSeparatorRightHTMLActive = value;
            }
        }

        public override string SeparatorRightHTMLBreadCrumb
        {
            get
            {
                return this.m_strSeparatorRightHTMLBreadCrumb;
            }
            set
            {
                this.m_strSeparatorRightHTMLBreadCrumb = value;
            }
        }

        public override bool SupportsPopulateOnDemand
        {
            get
            {
                return true;
            }
        }

        public override string WorkImage
        {
            get
            {
                return this.Menu.WorkImage;
            }
            set
            {
                this.Menu.WorkImage = value;
            }
        }
    }
}

